package com.bw.lib_common.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

/**
 * @author ytx
 * @date 2025-10-17 09:22
 * @description
 */
abstract class BaseFragment:Fragment() {
    private lateinit var mView:View
    private var isFirstLoaded = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mView = getFragmentRootView()
        if (!isFirstLoaded){
            firstInitData()
            isFirstLoaded = true
        }
        initView()
        return mView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initEnv()
        initData()
        initEvent()
    }

    override fun onResume() {
        super.onResume()
        refreshData()

    }

    /**
     * 第一次加载数据
     */
    protected open fun firstInitData(){

    }

    /**
     * 后续刷新数据
     */
    protected open fun refreshData(){

    }

    /**
     * 返回view
     */
    abstract fun getFragmentRootView():View

    /**
     * 初始化环境
     */
    protected open fun initEnv(){}

    /**
     * 初始化view
     */
    protected open fun initView(){}

    /**
     * 初始化数据
     */
    protected open fun initData(){}

    /**
     * 事件绑定、订阅
     */
    protected open fun initEvent(){}


    fun showMsg(msg:String){
        if (activity is BaseActivity){
            (activity as BaseActivity).showMsg(msg)
        }
    }
}